package com.tang.hawkmusic.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tang.hawkmusic.R;
import com.tang.hawkmusic.activity.MainActivity;
import com.tang.hawkmusic.activity.MainViewModel;
import com.tang.hawkmusic.activity.local.LocalActivity;
import com.tang.hawkmusic.activity.login.LoginActivity;
import com.tang.hawkmusic.adapter.PlayListAdapter;
import com.tang.hawkmusic.bean.PlaylistDTO;
import com.tang.hawkmusic.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends Fragment {
    TextView loginBtn;
    ConstraintLayout minwUserInfo;
    List<PlaylistDTO> createPlaylist;
    List<PlaylistDTO> collectPlaylist;
    RecyclerView createRecyclerView;
    RecyclerView collectRecyclerView;
    TextView createListName,collectListName;

    View localBtn;
    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_mine, container, false);
        loginBtn=view.findViewById(R.id.loginBtn);
        createRecyclerView=view.findViewById(R.id.create_playlist_recyclerview);
        collectRecyclerView=view.findViewById(R.id.collect_playlist_recyclerview);
        minwUserInfo=view.findViewById(R.id.mine_user_info);
        createListName=view.findViewById(R.id.create_list_name);
        collectListName=view.findViewById(R.id.collect_list_name);
        localBtn=view.findViewById(R.id.localBtn);
        initView();
        return view;
    }

    private void initView(){
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(getContext(), LoginActivity.class);
                startActivityForResult(intent,1);
            }
        });
        //打开本地音乐列表
        localBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(getContext(), LocalActivity.class);
                getActivity().startActivityForResult(intent,2);
            }
        });
    }

    public TextView getLoginBtn() {
        return loginBtn;
    }

    public ConstraintLayout getMinwUserInfo() {
        return minwUserInfo;
    }

    @SuppressLint("SetTextI18n")
    public void updatePlayList(List<PlaylistDTO> playlistDTOS) {
         createPlaylist=new ArrayList<>();
         collectPlaylist=new ArrayList<>();
        MainActivity mainActivity= (MainActivity) getActivity();
        MainViewModel mainViewModel=mainActivity.getMainViewModel();
        UserInfo userInfo=mainViewModel.getUserInfo();
        if (playlistDTOS!=null) {
            for (int i = 0; i < playlistDTOS.size(); i++) {
                if (playlistDTOS.get(i).getCreator().getUserId().equals(userInfo.getAccount().getId())) {
                    createPlaylist.add(playlistDTOS.get(i));
                } else collectPlaylist.add(playlistDTOS.get(i));
            }
            PlayListAdapter creatAdapter=new PlayListAdapter(mainActivity.getApplicationContext(),createPlaylist);
            PlayListAdapter collectAdapter=new PlayListAdapter(mainActivity.getApplicationContext(),collectPlaylist);
            createRecyclerView.setAdapter(creatAdapter);
            collectRecyclerView.setAdapter(collectAdapter);
            LinearLayoutManager linearLayoutManager1=new LinearLayoutManager(mainActivity.getApplicationContext());
            LinearLayoutManager linearLayoutManager2=new LinearLayoutManager(mainActivity.getApplicationContext());
            createRecyclerView.setLayoutManager(linearLayoutManager1);
            collectRecyclerView.setLayoutManager(linearLayoutManager2);
            createListName.setText("创建歌单("+createPlaylist.size()+")");
            collectListName.setText("收藏歌单("+collectPlaylist.size()+")");
        }

    }
}